package com.srpingde.hhh;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.springde.hhh.R;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.http.AsyncHttpResponseHandler;
import com.srpingde.hhh.jiekou.HHHIml;
import com.srpingde.hhh.jiekou.HHHInterface;

public class MoreActivity extends Activity {
	HHHIml weiQuIml;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		layout();
	}
	//版本更新
	private void update(){
		String updateUrl = HHHInterface.httpHeader + "api.php?tab=settings&act=data";
		Log.e("mytag", updateUrl+"==============");
		weiQuIml.banBenGengXin(MoreActivity.this,updateUrl,new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(final String content) {
				super.onSuccess(content);
				try {
					JSONObject jsonObject = new JSONObject(content);
					JSONArray array = jsonObject.getJSONArray("rows");
					for(int i = 0;i<array.length();i++){
					final JSONObject object = array.getJSONObject(i);
						if(!HhhApp.getInstance().getVersion(MoreActivity.this).substring(0, 1).equals(object.getString("version").substring(0, 1))){
							new AlertDialog.Builder(MoreActivity.this).setTitle("提示")
							.setMessage("唯趣有新版本了需要更新吗？")
							.setPositiveButton("立即更新",new DialogInterface.OnClickListener() {
								
								@Override
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									try {
										openExternalUrl(MoreActivity.this, HHHInterface.httpHeader+object.getString("apk"));
									} catch (JSONException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}
								}
							}).setNegativeButton("取消",null).create().show();
						}else{
							HhhApp.getInstance().showDialog(MoreActivity.this, "当前版本已经是最新版本");
						}
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
	}
	public void more(View v) {
		switch (v.getId()) {
		case R.id.more_update:
			update();
			break;
		case R.id.more_quite:
			//startActivity(new Intent(MoreActivity.this,LoginActivity.class));
			if(!HhhApp.getInstance().getUname().equals("")){
				new AlertDialog.Builder(MoreActivity.this).setTitle("提示").setMessage("确定退出吗？").setPositiveButton("确定", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						HhhApp.getInstance().clear();
						HhhApp.getInstance().showDialog(MoreActivity.this, "退出成功");
					}
				}).setNegativeButton("取消", null).create().show();
			}else{
				HhhApp.getInstance().showDialog(MoreActivity.this, "未登录");
			}
			
			break;
		case R.id.more_resetpassword:
			if(!HhhApp.getInstance().getUname().equals("")){
				startActivity(new Intent(MoreActivity.this,GengGaiMiMaActivity.class));
			}else{
				HhhApp.getInstance().showDialog(MoreActivity.this, "请先登录");
			}
			break;
		case R.id.more_about:
			startActivity(new Intent(MoreActivity.this,AboutActivity.class));
			break;
		case R.id.more_yijian:
			startActivity(new Intent(MoreActivity.this,OpinionActivity.class));
			break;
		}
	}
	public static void openExternalUrl(Context context, String url) {
		try {
			System.out.println("come in here????");
			Uri uri = Uri.parse(url);
			Intent intent = new Intent(Intent.ACTION_VIEW, uri);
			context.startActivity(intent);
		} catch (Exception e) {
			
		}
	}
	private void layout() {
		weiQuIml = new HHHIml();
		// 头的高度
		TextView top_title_tv = (TextView) findViewById(R.id.top_title);
		RelativeLayout.LayoutParams titleLayoutParams = (android.widget.RelativeLayout.LayoutParams) top_title_tv
				.getLayoutParams();
		titleLayoutParams.height = HhhApp.getInstance().title_h();
		top_title_tv.setLayoutParams(titleLayoutParams);
		
		itemLayout((Button) findViewById(R.id.more_about));
		itemLayout((Button) findViewById(R.id.more_resetpassword));
		itemLayout((Button) findViewById(R.id.more_quite));
		itemLayout((Button) findViewById(R.id.more_update));
		itemLayout((Button) findViewById(R.id.more_yijian));
	}
	private void itemLayout(Button  textView){
		LinearLayout.LayoutParams layoutParams = (android.widget.LinearLayout.LayoutParams) textView.getLayoutParams();
		layoutParams.height = (int) (HhhApp.getInstance().title_h()*1.5);
		textView.setLayoutParams(layoutParams);
	}
}
